package com.example.projecth.project3;

import com.example.projecth.project3.ChinUp.MyPagerAdapter;
import com.example.projecth.R;

import android.app.Fragment;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class ChinUp extends Fragment {

	private ViewPager pager;
	private TextView pageNo;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View view = inflater.inflate(R.layout.chinup, container, false);

		pager = (ViewPager) view.findViewById(R.id.pager);
		pageNo = (TextView) view.findViewById(R.id.pageNo);
		pager.setAdapter(new MyPagerAdapter(this));
		
		 pager.setOnPageChangeListener(new OnPageChangeListener() {
		
		 @Override
		 public void onPageSelected(int arg0) {
		 pageNo.setText(String.format("[ %d Page ]", arg0 + 1));
		
		 }
		
		 @Override
		 public void onPageScrolled(int arg0, float arg1, int arg2) {
		 // TODO Auto-generated method stub
		
		 }
		
		 @Override
		 public void onPageScrollStateChanged(int arg0) {
		 // TODO Auto-generated method stub
		
		 }
		 });

		return view;
	}
	
	
	
	

	class MyPagerAdapter extends PagerAdapter {

		ChinUp chinUp;
		LayoutInflater inflater;
		View[] views = new View[5];

		public MyPagerAdapter(ChinUp chinUp) {
			this.chinUp = chinUp;
			inflater = LayoutInflater.from(getActivity());
		}

		@Override
		public int getCount() {
			return views.length;
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return (arg0 == arg1);
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			if (views[position] == null) {
				switch (position) {
				case 0:
					views[0] = inflater.inflate(R.layout.chinup_sub1, null);
					break;
				case 1:
					views[1] = inflater.inflate(R.layout.chinup_sub2, null);
					break;
				case 2:
					views[2] = inflater.inflate(R.layout.chinup_sub3, null);
					break;
				case 3:
					views[3] = inflater.inflate(R.layout.chinup_sub4, null);
					break;
				case 4:
					views[4] = inflater.inflate(R.layout.chinup_sub5, null);
					break;
				}
			}
			container.addView(views[position]);
			return views[position];

		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView((View) object);

		}

	}
	@Override
	public void onPause() {
		
		super.onPause();
	}

}
